package com.eastfair.venueservice.service.impl;

import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.ExhibitionSettlementDetailMapper;
import com.eastfair.venueservice.entity.ExhibitionSettlementDetail;
import com.eastfair.venueservice.service.ExhibitionSettlementDetailService;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.database.mybatis.conditions.Wraps;
import com.eastfair.database.mybatis.conditions.query.QueryWrap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.util.Collection;

//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

import com.eastfair.venueservice.vo.*;
import com.eastfair.venueservice.dto.*;

/**
 * <p>
 * 业务实现类
 * 结算明细
 * </p>
 *
 * @author linan
 * @date 2023-05-25
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ExhibitionSettlementDetailServiceImpl extends SuperServiceImpl<ExhibitionSettlementDetailMapper, ExhibitionSettlementDetail> implements ExhibitionSettlementDetailService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ExhibitionSettlementDetail> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ExhibitionSettlementDetail model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public boolean saveBatch(Collection<ExhibitionSettlementDetail> entityList) {
        if(CollectionUtils.isNotEmpty(entityList)){
            for (ExhibitionSettlementDetail exhibitionSettlementDetail : entityList) {
                handlerSave(exhibitionSettlementDetail);
            }
            return this.saveBatch(entityList, 1000);
        }
        return false;
    }


    /**
     * 通用查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    public List<ExhibitionSettlementDetail> list(ExhibitionSettlementDetailDTO modelDTO, List<Long> ids) {
        return ((ExhibitionSettlementDetailService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
            magic = @Magic(
                    key = "{" +
                            "{'isEnabled', #args[0]?.isEnabled}," +
                            "{'settlementNo', #args[0]?.settlementNo}," +
                            "{'settlementType', #args[0]?.settlementType}," +
                            "{'businessNo', #args[0]?.businessNo}," +
                            "{'num', #args[0]?.num}," +
                            "{'unitPrice', #args[0]?.unitPrice}," +
                            "{'subtotalPrice', #args[0]?.subtotalPrice}," +
                            "{'id', #args[1]}" +
                            "}"))
    public List<ExhibitionSettlementDetail> listOfCache(ExhibitionSettlementDetailDTO modelDTO, List<Long> ids) {
        //查询
        ExhibitionSettlementDetail queryModel = new ExhibitionSettlementDetail();
        if (modelDTO != null) {
            BeanUtils.copyProperties(modelDTO, queryModel);
        }
        QueryWrap<ExhibitionSettlementDetail> wrapper = Wraps.q(queryModel);
        if (ids != null && !ids.isEmpty()) {
            wrapper.lambda().in(ExhibitionSettlementDetail::getId, ids);
        }
        //添加其他业务查询条件
        wrapper.lambda().eq(ExhibitionSettlementDetail::getIsDeleted, BusinessConstant.DELETE_NO);
        List<ExhibitionSettlementDetail> modelList = list(wrapper);
        return modelList;
    }


    /**
     * do转vo
     *
     * @param model model
     * @return modelVo
     */
    @Override
    public ExhibitionSettlementDetailVO doToVo(ExhibitionSettlementDetail model) {
        ExhibitionSettlementDetailVO modelVo = new ExhibitionSettlementDetailVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }

    /**
     * do转vo
     *
     * @param modelList modelList
     * @return list
     */
    @Override
    public List<ExhibitionSettlementDetailVO> doToVos(List<ExhibitionSettlementDetail> modelList) {
        if (modelList == null || modelList.isEmpty()) {
            return null;
        }
        List<ExhibitionSettlementDetailVO> result = new ArrayList<>();
        for (ExhibitionSettlementDetail model : modelList) {
            result.add(doToVo(model));
        }
        return result;
    }
}
